package com.cloud.form.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.form.api.entity.FormDef;
import com.cloud.form.api.mapper.FormDefMapper;
import com.cloud.form.api.service.FormInterfaceService;
import com.cloud.form.common.result.QkIcon;
import com.cloud.form.common.result.QkTreeNode;

@Service
@Transactional
public class FormInterfaceServiceImpl implements FormInterfaceService {

	@Autowired
	private FormDefMapper formDefMapper;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<QkTreeNode> listFormTree(long recId, long actDefId, String parts) {
		// 获取参与者在当前环节所有的文书权限
		List<FormDef> forms = formDefMapper.ListFormByActPart(actDefId, parts);
		List<Long> formIds = new ArrayList<Long>();
		for (FormDef form : forms) {
			formIds.add(form.getFormId());
		}
		// 获取已经填写的表单
		List<Long> filledFormIds = new ArrayList<Long>();
		if (formIds.size() > 0) {
			filledFormIds = jdbcTemplate
					.queryForList("select form_id from form_value where rel_key = ? and form_id in ("
							+ StringUtils.join(formIds, ",") + ")", Long.class, recId);
		}
		List<QkTreeNode> nodes = buildFormTreeList(forms);
		for (QkTreeNode node : nodes) {
			if (node.getIcon().indexOf("word") > 0 && filledFormIds.contains(Long.valueOf(node.getId()))) {
				node.setStyleClass("form-filled");
			}
		}
		return nodes;
	}

	@Override
	public List<QkTreeNode> listPrintFormTree(long recId, long actDefId, String parts) {
		// 已填写相关文书
		List<Long> filledFormIds = jdbcTemplate.queryForList("select form_id from form_value where rel_key = ?",
				Long.class, recId);
		// 获取参与者在当前环节所有的文书权限
		List<FormDef> authForms = formDefMapper.ListFormByActPart(actDefId, parts);
		List<FormDef> forms = new ArrayList<FormDef>();
		for (FormDef form : authForms) {
			if (filledFormIds.contains(form.getFormId())) {
				forms.add(form);
			}
		}
		// 返回结果树
		return buildFormTreeList(forms);
	}

	private List<QkTreeNode> buildFormTreeList(List<FormDef> forms) {
		List<QkTreeNode> results = new ArrayList<QkTreeNode>();
		List<String> groups = new ArrayList<String>();
		String formSeniorId = null;
		for (FormDef form : forms) {
			if (!StringUtils.isEmpty(form.getGroupName())) {
				String[] grpName = form.getGroupName().split("/");
				for (int i = 0; i < grpName.length; i++) {
					StringBuilder nodeId = new StringBuilder(grpName[0]);
					int j = 1;
					while (j <= i) {
						nodeId.append("_").append(grpName[j]);
						j++;
					}
					if (!groups.contains(nodeId.toString())) {
						QkTreeNode grpNode = new QkTreeNode(nodeId.toString(), grpName[i]);
						if (i > 0) {
							grpNode.setRootFlag(false);
							grpNode.setSeniorId(nodeId.substring(0, nodeId.lastIndexOf("_")));
						} else {
							grpNode.setRootFlag(true);
						}
						grpNode.setSelectable(false);
						grpNode.setExpanded(true);
						grpNode.setIcon(QkIcon.FA_FOLDER_OPEN);
						groups.add(nodeId.toString());
						results.add(grpNode);
					}
					formSeniorId = nodeId.toString();
				}
			}

			QkTreeNode formNode = new QkTreeNode(form.getFormId(), form.getFormName());
			formNode.setRootFlag(StringUtils.isEmpty(form.getGroupName()));
			formNode.setLeaf(true);
			formNode.setSelectable(true);
			formNode.setIcon(QkIcon.FA_FILE_WORD);
			formNode.setSeniorId(formSeniorId);
			results.add(formNode);
		}
		return results;
	}

	@Override
	public Long createCustomForm(Integer bizId, String bizName, Long procDefId) {
		FormDef formdef = new FormDef();
		formdef.setFormName(bizName);
		formdef.setBizId(bizId.longValue());
		formdef.setBizName(bizName);
		formdef.setProcDefId(procDefId);
		formdef.setProcDefName(bizName);
		formDefMapper.insert(formdef);
		return formdef.getFormId();
	}

}
